package application.employee.repository.impl;

import application.employee.model.Employee;
import application.employee.model.EmployeeDetail;
import application.employee.repository.EmployeeRepositoryCustom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.Collection;

/**
 * 雇员 数据层实现
 *
 * @author roamer
 * @version V1.0
 * @date 2019-03-03 14:31
 */
public class EmployeeRepositoryImpl implements EmployeeRepositoryCustom {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Collection<Employee> queryAll() {
        return mongoTemplate.findAll(Employee.class);
    }

    @Override
    public Employee updateEmployee(EmployeeDetail detail) {
        Query query = new Query().addCriteria(Criteria.where("_id").is(detail.getId()));
        Update update = new Update().set(Employee.DETAIL_FIELD, detail);
        return mongoTemplate.findAndModify(query, update, Employee.class);
    }
}
